import { defineStore } from "pinia";
import { reactive } from "vue";
import { userInfoAPI, resumeDetailsAPI, resumeDictAPI, labelsSkillAPI } from '@/api/my'

export const myStore = defineStore('my', () => {
    const state = reactive({
        userInfo: {},
        resumeInfo: {},
        companyList: [],
        projectList: [],

        experience: [],
        highestEducation: [],
        jobsType: [],
        salaryScope:<any> [],
        sysSkill: []
    })
    // 获取个人信息
    const getUserInfo = async() => {
        const res = await userInfoAPI()
        if (res) {
            state.userInfo = res
        }
    }
    // 获取个人简历详情信息
    const getResumeDetails = async() => {
        // 如果id不存在 调用接口 获得
        if (!state.userInfo.resume_id) await getUserInfo()
        const res = await resumeDetailsAPI({
            id: state.userInfo.resume_id
        })
        if (res) {
            state.companyList = res.data[0].companyList
            state.projectList = res.data[0].projectList
            state.resumeInfo = res.data[0].info[0]
        }
    }

    // 获取求职意愿字典
    const getresumeDict = async() => {
        const res = await resumeDictAPI() 
        if (res&& res.data) {
            state.experience = res.data.experience
            // 处理highestEducation
            state.highestEducation = res.data.highestEducation.map((item) => {
                return {
                    name:item.value
                }
            })
            state.jobsType = res.data.jobsType
            // 处理salaryScope
            let arr = []
            let scope = res.data.salary_scope
            for(let i=0; i < scope.length; i++) {
                arr.push({
                    name: scope[i]
                })
            }
            state.salaryScope = arr
        }
    }

    //获取简历技能关键字
    const getsysSkill = async() => {
        const res = await labelsSkillAPI()
        if (res) {
            state.sysSkill = res.data
        }
    }
    return {
        state,
        getUserInfo,
        getResumeDetails,
        getresumeDict,
        getsysSkill
    }
})